package com.boxGame.android;

import java.util.ArrayList;
import java.util.List;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Point;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.GridLayout;

public class GameView extends GridLayout {
	
	private final static int mColumCount = 4;
	private final static int mZero = 0;
	private final static int mSamllNum = 2;
	private final static int mBignum = 4;

	public GameView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		initGameView();
	}

	public GameView(Context context, AttributeSet attrs) {
		super(context, attrs);
		initGameView();
	}

	public GameView(Context context) {
		super(context);
		initGameView();
	}

	private void initGameView() {
		
		setColumnCount(mColumCount);
		setBackgroundColor(0xffbbada0);
		
		//���Ƽ���
		setOnTouchListener(new OnTouchListener() {
			
			private float startX, startY, offsetX, offsetY;
			
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				// TODO Auto-generated method stub
				
				switch (event.getAction()) {
				case MotionEvent.ACTION_DOWN:
					startX = event.getX();
					startY = event.getY();
					break;
				case MotionEvent.ACTION_MOVE:
					break;
				case MotionEvent.ACTION_UP:
					offsetX = event.getX() - startX;
					offsetY = event.getY() - startY;
					
					if (Math.abs(offsetX) > Math.abs(offsetY)) {
						if (offsetX < -5) {
							swipeLeft();
						} else if (offsetX > 5) {
							swipeRight();
						}
					} else {
						if (offsetY < -5) {
							swipeUp();
						} else if (offsetY > 5) {
							swipeDown();
						}
					}
					break;

				default:
					break;
				}
				return true;
			}
		});
	}
	
	private void swipeLeft() {
		
		boolean mMerge = false;
		
		for (int y = 0; y < mColumCount; y++) {
			for (int x = 0; x < mColumCount; x++) {
				
				for (int x1 = x + 1; x1 < mColumCount; x1++) {
					if (cardsMap[x1][y].getNum() > mZero) {
						if (cardsMap[x][y].getNum() <= 0) {
							cardsMap[x][y].setNum(cardsMap[x1][y].getNum());
							cardsMap[x1][y].setNum(mZero);
							mMerge = true;
							x--;
						} else if (cardsMap[x][y].equals(cardsMap[x1][y])) {
							cardsMap[x][y].setNum(cardsMap[x][y].getNum() * 2);
							cardsMap[x1][y].setNum(mZero);
							MainActivity.getAct().addScore(cardsMap[x][y].getNum());
							mMerge = true;
						}
						break;
					}
				}
			}
		}
		
		if (mMerge) {
			addRandomNum();
			checkComplete();
		}
	}
	
	private void swipeRight() {
		
		boolean mMerge = false;

		for (int y = 0; y < mColumCount; y++) {
			for (int x = mColumCount - 1; x >= 0; x--) {
				
				for (int x1 = x-1; x1 >= 0; x1--) {
					if (cardsMap[x1][y].getNum() > mZero) {
						if (cardsMap[x][y].getNum() <= 0) {
							cardsMap[x][y].setNum(cardsMap[x1][y].getNum());
							cardsMap[x1][y].setNum(mZero);
							mMerge = true;
							x++;
						} else if (cardsMap[x][y].equals(cardsMap[x1][y])) {
							cardsMap[x][y].setNum(cardsMap[x][y].getNum() * 2);
							cardsMap[x1][y].setNum(mZero);
							MainActivity.getAct().addScore(cardsMap[x][y].getNum());
							mMerge = true;
						}
						break;
					}
				}
			}
		}
		
		if (mMerge) {
			addRandomNum();
			checkComplete();
		}
	}
	
	private void swipeUp() {
		
		boolean mMerge = false;
		
		for (int x = 0; x < mColumCount; x++) {
			for (int y = 0; y < mColumCount; y++) {
				
				for (int y1 = y+1; y1 < mColumCount; y1++) {
					if (cardsMap[x][y1].getNum() > mZero) {
						if (cardsMap[x][y].getNum() <= 0) {
							cardsMap[x][y].setNum(cardsMap[x][y1].getNum());
							cardsMap[x][y1].setNum(mZero);

							mMerge = true;
							y--;
						} else if (cardsMap[x][y].equals(cardsMap[x][y1])) {
							cardsMap[x][y].setNum(cardsMap[x][y].getNum() * 2);
							cardsMap[x][y1].setNum(mZero);
							MainActivity.getAct().addScore(cardsMap[x][y].getNum());
							mMerge = true;
						}
						break;
					}
				}
			}
		}
		
		if (mMerge) {
			addRandomNum();
			checkComplete();
		}
	}
	
	private void swipeDown() {
		
		boolean mMerge = false;
		
		for (int x = 0; x < mColumCount; x++) {
			for (int y = mColumCount - 1; y >= 0; y--) {
				
				for (int y1 = y - 1; y1 >= 0; y1--) {
					if (cardsMap[x][y1].getNum() > mZero) {
						if (cardsMap[x][y].getNum() <= 0) {
							
							cardsMap[x][y].setNum(cardsMap[x][y1].getNum());
							cardsMap[x][y1].setNum(mZero);

							mMerge = true;
							y++;
						} else if (cardsMap[x][y].equals(cardsMap[x][y1])) {
							cardsMap[x][y].setNum(cardsMap[x][y].getNum() * 2);
							cardsMap[x][y1].setNum(mZero);
							MainActivity.getAct().addScore(cardsMap[x][y].getNum());
							mMerge = true;
						}
						break;
					}
				}
			}
		}
		
		if (mMerge) {
			addRandomNum();
			checkComplete();
		}
	}
	
	private void startGame() {

		MainActivity.getAct().clearScore();
		
		for (int y = 0; y < mColumCount; y++) {
			for (int x = 0; x < mColumCount; x++) {
				cardsMap[x][y].setNum(mZero);
			}
		}

		addRandomNum();
		addRandomNum();
	}
	
	private void checkComplete() {
		
		for (int y = 0; y < mColumCount; y++) {
			for (int x = 0; x < mColumCount; x++) {
				if (cardsMap[x][y].getNum() <= mZero ||
						(x > 0 && cardsMap[x][y].equals(cardsMap[x-1][y]))||
						(x < mColumCount - 1 && cardsMap[x][y].equals(cardsMap[x+1][y]))||
						(y > 0 && cardsMap[x][y].equals(cardsMap[x][y-1]))||
						(y < mColumCount - 1 && cardsMap.equals(cardsMap[x][y+1]))) {
					return;
				}
			}
		}
		
		//��Ϸ����
		new AlertDialog
		.Builder(getContext())
		.setTitle("��Ϸ����")
		.setMessage("��Ϸ�����Ƿ�Ҫ���¿�ʼ��")
		.setPositiveButton("ȡ��", null)
		.setNegativeButton("ȷ��", new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				// TODO Auto-generated method stub
				startGame();
			}
		}).show();
	}
	
	private Card cardsMap[][] = new Card[4][4];
	private List<Point> emptyPoints = new ArrayList<Point>();
	private void addCard(int cardWidth, int cardHeight) {
		
		Card c;
		
		for (int y = 0; y < mColumCount; y++) {
			for (int x = 0; x < mColumCount; x++) {
				c = new Card(getContext());
				c.setNum(2);
				addView(c, cardWidth, cardHeight);
				cardsMap[x][y] = c;
			}
		}
	}
	
	private void addRandomNum() {
		
		emptyPoints.clear();
		for (int y = 0; y < mColumCount; y++) {
			for (int x = 0; x < mColumCount; x++) {
				if (cardsMap[x][y].getNum() <= 0) {
					emptyPoints.add(new Point(x, y));
				}
			}
		}
		
		Point p = emptyPoints.remove((int)(Math.random()*emptyPoints.size()));
		cardsMap[p.x][p.y].setNum(Math.random() > 0.1f?mSamllNum:mBignum);
	}
	
	@Override
	protected void onSizeChanged(int w, int h, int oldw, int oldh) {
		// TODO Auto-generated method stub
		super.onSizeChanged(w, h, oldw, oldh);
		
		int cardWidth = (Math.min(w, h) - 25) / 4;
		addCard(cardWidth, cardWidth);
		
		startGame();
	}
}
